/* Data0.do (STATA)
	Construct characteristics data.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;
clear all;
set more off;
set type double;

cap log close;
log using Data0, replace;


/* Load IMF IFS data */

u year country fx NCP_R_XDC
	using IMF_IFS, clear;

/* Merge World Bank data */

merge 1:1 year country using WorldBank,
	keepusing(outstand cpi consR gdp gdpP gdpPpc PANUSPPP pop)
	nogen;

/* Merge Capital IQ Ratings data */

merge 1:1 year country using Ratings,
	keepusing(rating drate)
	nogen;

/* Merge Datastream data */

merge 1:1 year country using Datastream,
	keepusing(IR3M IR10Y vol retIR3M retMS)
	nogen;

/* Merge MSCI data */

merge 1:1 year country using MSCI,
	nogen;

/* Merge country code */

merge m:1 country using Countries,
	keepusing(Name MSCI Yeuro Ynat)
	nogen keep(match);

/* Construct variables */

gen Counterpart = country;
gen counterpart = country;

gen byte Ieuro = year>=Yeuro;

drop Yeuro;

/* Construct relative price */

egen PANUSPPP1 = total(PANUSPPP*gdp*Ieuro) if !missing(PANUSPPP), missing by(year);
egen PANUSPPP2 = total(gdp*Ieuro) if !missing(PANUSPPP), missing by(year);

gen relpi = PANUSPPP;
replace relpi = PANUSPPP1/PANUSPPP2 if Ieuro;	/* GDP-weighted average for euro area */

replace relpi = PANUSPPP1/PANUSPPP2 if country=="DNK";	/* Denmark pegged to euro */
replace relpi = 1 if country=="HKG";					/* Hong Kong pegged to USD */

drop PANUSPPP*;

/* Construct real exchange rate */

gen realfx = fx/relpi;

/* Construct interest rates */

gen yieldST = ln(1+IR3M);
gen yieldLT = ln(1+IR10Y);

drop IR3M IR10Y;

/* Construct growth rates */

sort country year;

foreach var of varlist cpi  {;
	by country: gen G`var' = ln(`var'/`var'[_n-1]);

	label var G`var' "Log growth rate of `var'";
};

/* Construct future growth rates */

foreach var of varlist fx NCP_R_XDC cpi consR pop {;
	by country: gen _G`var' = ln(`var'[_n+1]/`var');

	label var _G`var' "Future: Log growth rate of `var'";
};

/* Construct macro variables */

gen _GconsRpc = _GconsR-_Gpop;								/* World Bank */
replace _GconsRpc = _GNCP_R_XDC-_Gpop if missing(_GconsR);	/* IMF IFS */

/* Construct future returns */

foreach var of varlist retIR3M retMS {;
	by country: gen _`var' = `var'[_n+1];
};

/* Keep sample of countries */

keep if year>=Ynat;

drop Ynat;

/* Label variables */

order year country Counterpart counterpart Name MSCI Ieuro;
order relpi realfx, after(fx);

label var Counterpart	"Issuer country by residency";
label var counterpart	"Issuer country by nationality";
label var Ieuro			"Dummy: Euro area";

label var relpi			"Relative price index (US$ per LCU)";
label var realfx		"Real exchange rate (fx/relpi)";

label var yieldST		"3-month interbank rate (continuously compounded)";
label var yieldLT		"10-year benchmark government yield (continuously compounded)";

label var _GconsRpc		"Future: Log growth rate of real consumption per capita";

label var _retIR3M		"Future: Interest rate (LCU, continuously compounded)";
label var _retMS		"Future: Stock return (US$, continuously compounded)";

/* Save data */

sort year country;

save Data0, replace;

log close;
